<template>
  <el-card>
    <div class="changeword">
      <h2 class="applyRecode">修改密码</h2>
      <el-form class="form" :model="form" :rules="rules" ref="form" label-width="110px">
        <el-form-item label="原密码：" prop="oldPasswd">
          <el-input placeholder="请输入原密码" v-model="form.oldPasswd"></el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPasswd">
          <el-input placeholder="密码8-32位，必须同时大小写 + 数字组合" v-model="form.newPasswd"></el-input>
        </el-form-item>
        <el-form-item label="确认密码：" prop="surepwd">
          <el-input placeholder="密码8-32位，必须同时大小写 + 数字组合" v-model="form.surepwd"></el-input>
        </el-form-item>
      </el-form>
      <div class="btns">
        <el-button type="primary" size="small" @click="cancel">取消</el-button>
        <el-button type="primary" size="small" @click="init">确认</el-button>
      </div>
    </div>
  </el-card>
</template>
<script>
export default {
  name: 'changeword',
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.newPasswd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        oldPasswd: '',
        newPasswd: '',
        surepwd: ''
      },
      rules: {
        oldPasswd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { pattern: /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{8,32}$/, message: '密码8-32位，必须同时大小写+数字组合', trigger: 'blur' }
        ],
        newPasswd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{8,32}$/, message: '密码8-32位，必须同时大小写+数字组合', trigger: 'blur' }
        ],
        surepwd: [
          { validator: validatePass2, trigger: 'blur' },
          { pattern: /^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{8,32}$/, message: '密码8-32位，必须同时大小写+数字组合', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // console.log('aaa')
  },
  methods: {
    init () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$tkAjax.post(`${this.$config.baseUrl}/user/merchantUser/resetMerchantPasswd`, {}, {
            params: {
              userId: this.$store.state.userId,
              oldPasswd: this.$md5(this.form.oldPasswd),
              newPasswd: this.$md5(this.form.newPasswd)
            }
          }).then(res => {
            this.$message.success(res.data.content)
          }).catch(err => {
            this.$message.warning(err.data.message)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel () {
      this.$router.push('/')
    }
  }
}
</script>
<style lang="scss" scoped>
.changeword {
  .form {
    width: 50%;
    color: #000;
    margin: 0 auto;
  }
  .btns {
    // width: 50%;
    text-align: center;
    margin: 42px 0 14px;
  }
}
</style>
